import ply.yacc as yacc

from calu_lex import tokens

precedence = (
	('left', 'PLUS', 'MINUS'),
	('left', 'TIMES', 'DIV'),
)

def p_binary_operator (p):
	'''expression : expression PLUS  expression
			   	  | expression MINUS expression
				  | expression TIMES expression
				  | expression DIV	 expression'''
	if p[2] == '+' :
		p[0] = p[1] + p[3]
	elif p[2] == '-' :
		p[0] = p[1] - p[3]
	elif p[2] == '*' :
		p[0] = p[1] * p[3]
	elif p[2] == '/' :
		p[0] = p[1] / p[3]

def p_expression (p):
	'expression : LPAREN expression RPAREN'
	p[0] = p[2]

def p_factor_num (p):
	'expression : NUMBER'
	p[0] = p[1]

def p_error (p):	
	print "syntax error!"

parser = yacc.yacc();

while True:
	try:
		s = raw_input('calu > ')
	except EOFError:
		break
	if not s: continue
	result = parser.parse(s)
	print result

